package org.sinuto.documentserver.service.impl;

import lombok.SneakyThrows;
import org.apache.commons.lang3.CharEncoding;
import org.sinuto.documentserver.exception.DocumentServerException;
import org.sinuto.documentserver.manager.CommandManager;
import org.sinuto.documentserver.service.CommandService;
import org.sinuto.documentserver.service.FileService;
import org.sinuto.documentserver.utils.CustomMap;
import org.sinuto.documentserver.utils.DocumentServerUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.net.URLDecoder;

/**
 * @Author 2023/3/1 18:00 ly
 **/
@Service
public class CommandServiceImpl implements CommandService {

    @Resource
    private DocumentServerUtils documentServerUtils;
    @Resource
    private CommandManager commandManager;

    @Resource
    private FileService fileService;

    @Override
    public String forceSave(String id) throws DocumentServerException {
        String filePath = fileService.getFilePath(id);
        return commandManager.forceSave(documentServerUtils.key(new File(filePath)), "simple data");
    }

    @SneakyThrows
    @Override
    public String drop(String id, String user) throws DocumentServerException {
        String filePath = fileService.getFilePath(id);
        return commandManager.drop(documentServerUtils.key(new File(filePath)), new String[]{"uid-" + URLDecoder.decode(user, CharEncoding.UTF_8)});
    }

    @Override
    public String info(String id) throws DocumentServerException {
        String filePath = fileService.getFilePath(id);
        return commandManager.info(documentServerUtils.key(new File(filePath)));
    }

    @Override
    public String license() {
        return commandManager.license();
    }

    @Override
    public String meta(String id) throws DocumentServerException {
        String filePath = fileService.getFilePath(id);
        return commandManager.meta(documentServerUtils.key(new File(filePath)), CustomMap.build(1).pu1("title", "test meta.docx"));
    }

    @Override
    public String version() {
        return commandManager.version();
    }
}
